﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CarErp.Model;

namespace CarErp.Forms.Controls
{
	public partial class CarPurchaseApplicationView : UserControl
	{
		public CarPurchaseApplicationView()
		{
			InitializeComponent();
			Reset();
		}

		public void Reset()
		{
			Model = null;

			textBox_Id.Text = "";
			textBox_ApplicantCompany.Text ="";
			dateTimePicker_ApplicantTime.Value = DateTime.Now;
			textBox_CarName.Text = "";
			textBox_Brand.Text = "";
			textBox_Specification.Text = "";
			textBox_ModelNumber.Text = "";
			textBox_Maker.Text = "";
			textBox_Quantity.Text = "0";
			textBox_BudgetUnitPrice.Text = "0";
			dateTimePicker_PlannedAcquisitionDate.Value = DateTime.Now;
			textBox_Appendix.Text = "";
			textBox_SourceOfFunds.Text = "";


		}

		public void SetModel(CarPurchaseApplication model)
		{
			Model = model;

			textBox_Id.Text = model.Id.ToString();
			textBox_ApplicantCompany.Text = model.ApplicantCompany;
			dateTimePicker_ApplicantTime.Value = model.ApplicantTime;
			textBox_CarName.Text = model.CarName;
			textBox_Brand.Text = model.Brand;
			textBox_Specification.Text = model.Specification;
			textBox_ModelNumber.Text = model.ModelNumber;
			textBox_Maker.Text = model.Maker;
			textBox_Quantity.Text = model.Quantity.ToString();
			textBox_BudgetUnitPrice.Text = model.BudgetUnitPrice.ToString();
			dateTimePicker_PlannedAcquisitionDate.Value = model.PlannedAcquisitionDate;
			textBox_Appendix.Text = model.Appendix;
			textBox_SourceOfFunds.Text = model.SourceOfFunds;
			radio_0.Checked = model.PropertyRight == PropertyRight.CompanyOwned;
			radio_1.Checked = model.PropertyRight == PropertyRight.LinkedFinancing;
		}

		public CarPurchaseApplication GetModel()
		{
			var result = Model ?? new CarPurchaseApplication();
			result.ApplicantCompany = textBox_ApplicantCompany.Text;
			result.ApplicantTime = dateTimePicker_ApplicantTime.Value;
			result.CarName = textBox_CarName.Text;
			result.Brand = textBox_Brand.Text;
			result.Specification = textBox_Specification.Text;
			result.ModelNumber = textBox_ModelNumber.Text;
			result.Maker = textBox_Maker.Text;
			result.Quantity = int.Parse(textBox_Quantity.Text);
			result.BudgetUnitPrice = decimal.Parse(textBox_BudgetUnitPrice.Text);
			result.PlannedAcquisitionDate = dateTimePicker_PlannedAcquisitionDate.Value;
			result.Appendix = textBox_Appendix.Text;
			result.SourceOfFunds = textBox_SourceOfFunds.Text;
			result.Submitter = Model?.Submitter;
			result.ApprovalPaper = Model?.ApprovalPaper;
			result.PropertyRight = radio_0.Checked ? PropertyRight.CompanyOwned : PropertyRight.LinkedFinancing;

			return result;
		}


		private CarPurchaseApplication Model;
	}
}
